* specification tests


* controls options
local controls_lla0 "" // IFO LS i.dow // unrate i.dow i.q  // i.m i.q // built dwt loa draft beam power_kw USflag
local controls_lla1 IFO LS // unrate i.dow i.q  // i.m i.q // built dwt loa draft beam power_kw USflag
local controls_lla2 IFO LS unrate // i.m i.q // built dwt loa draft beam power_kw USflag
*local controls_lla3 IFO LS c.unrate##c.unrate // i.m i.q // built dwt loa draft beam power_kw USflag

local controls_ves built dwt loa draft beam power_kw USflag group_agg_i

local vce "cluster ves_id"

local rte_opts all_exits all_coast
local folder rd_regs

local eca_ys 2009 // 2011
local exposeds 1	
local folder rd_regs
local vestype "Container"	
local vestype2 : subinstr local vestype " " ""	

* create time index that drops 26 days prior
capture drop t_eca09_pre26dropped
gen t_eca09_pre26dropped = t_eca09
replace t_eca09_pre26dropped =  t_eca09_pre26dropped+26 if t_eca09_pre26dropped<0

capture drop reg_samp

foreach rte_opt of local rte_opts {

	foreach eca_y of local eca_ys {
		if `eca_y' == 2009 {
			local run t_eca09
			local cut eca1
			local y_tag "09"
			
			* set sample criteria
			local bw 150
			local donut 0 
			local ports `" inlist(port_agg,"SoCal","NoCal") "'
			
			* placebo
			*local run_p t_eca_d1
			*local cut_p cut_d1
			
			* 
			local run_drop26 t_eca09_pre26dropped
			
		}
		if `eca_y'==2011 {
			local run t_eca11 
			local cut eca2
			local y_tag "11"
			
			* set sample criteria
			local bw 150
			local donut 0
			local ports `" inlist(port_agg,"SoCal") "' // only socal 
			
			*placebo
			*local run_p t_eca_d2
			*local cut_p cut_d2
			
			local run_drop26 t_eca11
		}
		
		* default route fixed effects
		local rte_fe route_id
		if "`rte_opt'"=="all_coast" {
			local rte_cond 	`" `ports' & route_type=="Coastal" "' 
			local samp_lab "port-port"
		}
				
		if "`rte_opt'"=="all_exits" {
			local rte_cond 	`" `ports' & route_type=="EnterExit"  "' 
			* local rte_fe route_agg // collapsing exit routes
			local samp_lab "ent/exit"
		}	
			
		local fe_vesrte "i.`rte_fe'#i.ves_id"	
		local fe_rte "i.`rte_fe'"	
		local fe_oprte "i.`rte_fe'#i.operator_group_i"	
		
		local xvar "i.cut#c.`run'" 
		local xvar_rte "i.cut#c.`run'#i.`rte_fe'"
		local xvar_rte_quad "i.cut#(c.`run'##c.`run')#i.`rte_fe'"
		
		local xvar_rte_drop26 "i.cut#c.`run_drop26'#i.`rte_fe'"
		
		*** Y variables
		if "`y_tag'"=="09" {
			*local yvars dist_eca20`y_tag' dist f_eca`y_tag'_cons f_cons cost_comply_cons td_inm_comply_cons cost_eca`y_tag'_cons td_inm_eca`y_tag'_cons 
			local yvars dist_eca`eca_y' avg_speed_eca`eca_y' f_eca`y_tag'_cons cost_eca`y_tag'_cons e_pm_eca`y_tag'_cons td_inm_eca`y_tag'_cons 
		}
		else {
			*local yvars dist_eca20`y_tag' dist f_eca`y_tag'_cons f_cons cost_comply_cons td_inm_comply_cons
			local yvars dist_eca`eca_y' avg_speed_eca`eca_y' f_eca`y_tag'_cons cost_eca`y_tag'_cons e_pm_eca`y_tag'_cons td_inm_eca`y_tag'_cons 
		}
		
		capture drop samp_ind
		gen samp_ind = (sample_RD_regs==1) & `rte_cond' & vesseltype_regstr=="`vestype'" 
				
		foreach yvar of local yvars {
			local apprep replace
			local out_file "`folder'/ll_controls_levels_`yvar'_`rte_opt'_`vestype2'_eca`eca_y'.tex"	
			
			capture drop cut
			gen cut = `cut'
			local lbl : variable label `cut'
			label var cut `"`lbl'"' 
			
			* hetero trends - replicating above
			reghdfe `yvar' c.cut `xvar_rte' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' )  ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
					addstat( Vessels , e(N_clust))  /// 
					addtext( Bandwidth, `bw',  Adj Time Index, "~", Route-by-Vessel FE, Y , Route-by-Operator FE , "~" , Route FE , "~" , Route Trends, Y, Fuel Prices , Y, Vessel Controls, "~" , Sample, Main) ctitle("") // Quad Route Trends, "~",
			local apprep append
			capture drop reg_samp
			gen reg_samp = e(sample) // saving sample
						
			* time index that drops 30 days prior			
			reghdfe `yvar' c.cut `xvar_rte_drop26' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' )  ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
					addstat( Vessels , e(N_clust))  /// 
					addtext( Bandwidth, `bw',  Adj Time Index, Y , Route-by-Vessel FE, Y , Route FE , "~" , Route Trends, Y, Fuel Prices , Y, Vessel Controls, "~" , Sample, Main) ctitle("") // Quad Route Trends, "~",
						
			/*
			* adding 30 day donut
			reghdfe `yvar' c.cut `xvar_rte' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' )  ///
						& ( `run'<-30 | `run'>30 ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
					addstat( Vessels , e(N_clust))  /// 
					addtext( Bandwidth, `bw', Donut, 30 , Route-by-Vessel FE, Y , Route FE , "~" , Route Trends, Y, Fuel Prices , Y, Vessel Controls, "~" , Sample, Main) ctitle("") // Quad Route Trends, "~",
			*/
			
			* dropping fuel prices 
			reghdfe `yvar' c.cut `xvar_rte' `controls_lla0' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' ) ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
						addstat( Vessels , e(N_clust))  addtext(Bandwidth, `bw',  Route-by-Vessel FE, Y , Route Trends, Y , Fuel Prices , N, Sample, Main ) ctitle("")
					
			* quadratic route trends	
			/*
			reghdfe `yvar' c.cut `xvar_rte_quad' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' ) ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
						addstat( Vessels , e(N_clust))  addtext( Bandwidth, `bw', Donut, `donut', Route-by-Vessel FE, Y  , Route FE , "~" ,  Quad Route Trends, Y, Sample, Main) ctitle("")
			*/
			
			* homogeneous trends 
			reghdfe `yvar' c.cut `xvar' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' ) ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
						addstat( Vessels , e(N_clust))  addtext( Bandwidth, `bw',  Route-by-Vessel FE, Y  , Route FE , "~" ,  Route Trends, N, Fuel Prices , Y, Sample, Main) ctitle("")	
							
			/*
			reghdfe `yvar' c.cut `xvar_rte' `controls_lla2' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' ) ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
						addstat( Vessels , e(N_clust))  addtext(Bandwidth, `bw',  Route Trends, Y,  Fuel Prices, Y, Unem Rate, Y) ctitle("")
			*/
			
			* Limit Sample to vessels we observe both pre and post
			capture drop tmp_*
			gen tmp_pre = (`run'>=-`bw') & (`run'<-`donut')
			egen tmp_cnt_pre = total(tmp_pre) , by(route_id ves_id)
			gen tmp_post = (`run'<=`bw') & (`run'>`donut')
			egen tmp_cnt_post = total(tmp_post) , by(route_id ves_id)
			gen tmp_pre_post = tmp_cnt_post>0 & tmp_cnt_pre>0 
			
			reghdfe `yvar' c.cut `xvar_rte' `controls_lla1' if samp_ind==1 & tmp_pre_post & ( `run'<=`bw' & `run'>=-`bw' )  ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
					addstat( Vessels , e(N_clust))  addtext( Bandwidth, `bw',  Route-by-Vessel FE, Y ,  Route Trends, Y, Fuel Prices , Y, Sample, Pre+Post) ctitle("")
			drop tmp_*	
				
				
			* operator by route fe sample, but with vessel fixed effects
			capture drop samp_op
			gen samp_op = ( reg_samp==1 ) & ( operator_group_i~=. )
			
			* operator sample vessel by route fe
			reghdfe `yvar' c.cut `xvar_rte' `controls_lla1' if samp_op==1  & ( `run'<=`bw' & `run'>=-`bw' ) ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
						addstat( Vessels , e(N_clust))  addtext( Bandwidth, `bw',  Route-by-Vessel FE , Y , Route Trends, Y , Fuel Prices , Y, Sample, Operator) ctitle("")
			
			
			* same sample but operator by route fe
			reghdfe `yvar' c.cut `xvar_rte' `controls_lla1' if samp_op==1  & ( `run'<=`bw' & `run'>=-`bw' ) ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_oprte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
						addstat( Vessels , e(N_clust))  addtext( Bandwidth, `bw',  Route-by-Operator FE , Y , Route Trends, Y , Fuel Prices , Y, Sample, Operator) ctitle("")
			
			
			
			/*
			* same sample but operator by route fe with vessel controls
			reghdfe `yvar' c.cut `xvar_rte' `controls_ves' `controls_lla1' if reg_samp==1  & ( `run'<=`bw' & `run'>=-`bw' ) ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_oprte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
						addstat( Vessels , e(N_clust))  addtext( Bandwidth, `bw',  Route-by-Operator FE , Y , Route Trends, Y , Fuel Prices , Y, Vessel Controls, Y , Sample, Main) ctitle("")
			*/
						
				
			* same sample but route fe and vessel fe
			reghdfe `yvar' c.cut `xvar_rte' `controls_lla1' if reg_samp==1  & ( `run'<=`bw' & `run'>=-`bw' ) ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_rte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
						addstat( Vessels , e(N_clust))  addtext( Bandwidth, `bw',  Route FE , Y , Route Trends, Y , Fuel Prices , Y, Sample, Main) ctitle("")
		
				
			* same sample but route fe and vessel controls
			reghdfe `yvar' c.cut `xvar_rte' `controls_ves' `controls_lla1' if reg_samp==1  & ( `run'<=`bw' & `run'>=-`bw' ) ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_rte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
						addstat( Vessels , e(N_clust))  addtext( Bandwidth, `bw',  Route FE , Y , Route Trends, Y , Fuel Prices , Y, Vessel Controls, Y , Sample, Main) ctitle("")

			* larger sample 
			/*
			reghdfe `yvar' c.cut `xvar_rte' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' ) ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_rte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
						addstat( Vessels , e(N_clust))  addtext( Bandwidth, `bw',  Route FE , Y , Route Trends, Y , Fuel Prices , Y,  Sample, Full ) ctitle("")
			
			reghdfe `yvar' c.cut `xvar_rte' `controls_ves' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' ) ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_rte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
						addstat( Vessels , e(N_clust))  addtext( Bandwidth, `bw',  Route FE , Y , Route Trends, Y , Fuel Prices , Y, Vessel Controls, Y , Sample, Full ) ctitle("")
			*/
			
			/*
			* these should be tracks that are only in larger sample
			reghdfe `yvar' c.cut `xvar_rte' `controls_ves' `controls_lla1' if samp_ind==1 & reg_samp~=1 & ( `run'<=`bw' & `run'>=-`bw' ) ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_rte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
						addstat( Vessels , e(N_clust))  addtext( Bandwidth, `bw',  Route FE , Y , Route Trends, Y , Fuel Prices , Y, Vessel Controls, Y , Sample, Singles ) ctitle("")
			*/
			
			
			/*
			* placebo ( add a year or subtract a year)	
			capture drop cut
			gen cut = `cut_p'
			label var cut `"`lbl'"' 
			
			reghdfe `yvar' c.cut `xvar_rte' `controls_lla1' if samp_ind==1 & ( `run_p'<=`bw' & `run_p'>=-`bw' )  ///
						& ( `run_p'<-`donut' | `run_p'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
						addstat( Vessels , e(N_clust))  addtext( Bandwidth, `bw',  Route-by-Vessel FE, Y ,  Route Trends, Y, Sample, Placebo) ctitle("")
			*/
						
			drop reg_samp
		}	
		drop samp_ind
	}
}


/*
	foreach exposed of local exposeds {
		
		* set yvariables
		if `exposed'==1 {
			*local yvars ldist_eca`eca_y' ldist lf_cons 
			local yvars dist_eca`eca_y' dist f_cons   // Change name of table below
		}
		else {
			*local yvars ldist lf_cons 
			local yvars dist f_cons // change name of table below
		}
*/
